package org.lanqiao.webfm.dao;

import java.util.*;
import java.io.Serializable;
import java.sql.*;
import java.lang.reflect.*;

import javacommon.base.*;
import javacommon.ijdbc.*;
import javacommon.util.*;
import javacommon.web.page.*;

import org.lanqiao.webfm.model.*;

import org.slf4j.*;

/**
*@Author zhengwei
**/
public class UserinfoDao extends BaseJdbcDao<Userinfo>{
	private static final Logger LOGGER = LoggerFactory.getLogger(UserinfoDao.class);
	private UserinfoDao(){}
	private static UserinfoDao instance = new UserinfoDao();
	public static UserinfoDao getInstance(){
		return instance;
	}
	public Class getEntityClass() {
		return Userinfo.class;
	}
	
	/*public RowMapper<Userinfo> getMapper() {
		return new UserinfoMapper();
	}*/
	public String getIdColumnName() {
		return "ID";
	}
	public String getTableName() {
		return "SP_USERINFO";
	}
	
	public Userinfo getById(Serializable id) throws SQLException {
		String sql = "select * from SP_USERINFO t1 "
				+ "where t1.ID=?";
        return operation.queryForUniqueBean(sql,Userinfo.class, id);
	}
	@Override
	public void save(Userinfo entity) throws SQLException {
		StringBuilder sql = new StringBuilder("insert into SP_USERINFO")
				.append(" (")
				.append("ID").append(",")
				.append("USERNAME").append(",")
				.append("PWD").append(",")
				.append("EMAIL").append(",")
				.append("REALNAME").append(",")
				.append("GENDER").append(",")
				.append("QUESTION").append(",")
				.append("ANSWER").append(",")
				.append("PHONE").append(",")
				.append("MYPAGE").append(",")
				.append("HOBBIES").append(",")
				.append("SELFINTRO")
				.append(") ")
				.append("values(dev_seq.nextval,")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?")
				.append(")");
			
		super.operation
				.execute(sql.toString(), 
								entity.getUsername(),
								entity.getPwd(),
								entity.getEmail(),
								entity.getRealname(),
								entity.getGender(),
								entity.getQuestion(),
								entity.getAnswer(),
								entity.getPhone(),
								entity.getMypage(),
								entity.getHobbies(),
								entity.getSelfintro()
				);
	}
	@Override
	public void update(Userinfo entity) throws SQLException {
		StringBuilder sql = new StringBuilder("update SP_USERINFO")
				.append(" set ") 
				.append("USERNAME=?").append(",")
				.append("PWD=?").append(",")
				.append("EMAIL=?").append(",")
				.append("REALNAME=?").append(",")
				.append("GENDER=?").append(",")
				.append("QUESTION=?").append(",")
				.append("ANSWER=?").append(",")
				.append("PHONE=?").append(",")
				.append("MYPAGE=?").append(",")
				.append("HOBBIES=?").append(",")
				.append("SELFINTRO=?")
				.append(" where ID=?");
		super.operation
				.execute(sql.toString(),
							entity.getUsername(),
							entity.getPwd(),
							entity.getEmail(),
							entity.getRealname(),
							entity.getGender(),
							entity.getQuestion(),
							entity.getAnswer(),
							entity.getPhone(),
							entity.getMypage(),
							entity.getHobbies(),
							entity.getSelfintro(),
						entity.getId());
	}
	/**
	 * 根据query对象来查询数据，结果取决于query中有查询条件及query中的分页信息
	 */
	public Page<Userinfo> findPage(PageRequest<Userinfo> query) throws SQLException{
		Userinfo userinfo = (Userinfo) query;
        StringBuilder sql = new StringBuilder("select t.* from SP_USERINFO t where 1=1 ");
        if(StringUtils.hasLength(userinfo.getId())) {
         	sql.append(" and  t.ID =  ").append(userinfo.getId());
        }
        if(StringUtils.hasLength(userinfo.getUsername())) {
            sql.append(" and  t.USERNAME =  '").append(userinfo.getUsername()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getPwd())) {
            sql.append(" and  t.PWD =  '").append(userinfo.getPwd()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getEmail())) {
            sql.append(" and  t.EMAIL =  '").append(userinfo.getEmail()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getRealname())) {
            sql.append(" and  t.REALNAME =  '").append(userinfo.getRealname()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getGender())) {
         	sql.append(" and  t.GENDER =  ").append(userinfo.getGender());
        }
        if(StringUtils.hasLength(userinfo.getQuestion())) {
            sql.append(" and  t.QUESTION =  '").append(userinfo.getQuestion()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getAnswer())) {
            sql.append(" and  t.ANSWER =  '").append(userinfo.getAnswer()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getPhone())) {
            sql.append(" and  t.PHONE =  '").append(userinfo.getPhone()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getMypage())) {
            sql.append(" and  t.MYPAGE =  '").append(userinfo.getMypage()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getHobbies())) {
            sql.append(" and  t.HOBBIES =  '").append(userinfo.getHobbies()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getSelfintro())) {
            sql.append(" and  t.SELFINTRO =  '").append(userinfo.getSelfintro()).append("'");
        }
        if(StringUtils.hasLength(userinfo.getSortColumns())) {
            sql.append(" order by ").append(userinfo.getSortColumns());
        }	
        
		return pageQuery(sql.toString(),userinfo);
	}
	
	public void deleteByIds(String[] ids) throws SQLException {
		String[] sqls = new String[ids.length];
		for (int i = 0; i < sqls.length; i++) {
			sqls[i] = "delete from SP_USERINFO where ID=" + ids[i];
		}
		operation.executeBatch(sqls);
	}

}
